package com.abo.yaas.webservice.client;

import com.abo.yaas.domain.User;
import com.abo.yaas.webservice.constants.URLConstants;
import com.abo.yaas.webservice.exceptions.CommunicationException;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

@SuppressWarnings("serial")
public class UserProviderProxy extends EntityProviderProxy<User> {
    
    public UserProviderProxy(WebResource resource) {
        super(User.class, resource);
    }

    public User authenticate(final String login, final String password) {
        final WebResource res = servicePath(URLConstants.authentication);
        res.addFilter(new HTTPBasicAuthFilter(login, password));
        return res.get(User.class);
    }

    public User register(final User user) throws CommunicationException {
        User result = null;
        try { 
            result = servicePath(URLConstants.register).post(User.class, user);
        } catch (final UniformInterfaceException e) {
            throw new CommunicationException(e);
        }
        return result;
    }
}
